package oa.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import oa.biz.EmployeeBiz;
import oa.entity.Employee;

@Controller
public class UserController {
	@Autowired
	private EmployeeBiz emp;

	@RequestMapping("/check-login")
	public String checkUser(String nr, String password, Model model, HttpSession session) {
		Employee employee = emp.fetchByUser(nr, password);
		if (employee != null) {
			session.setAttribute("employee", employee);
			return "redirect:/admin/index";
		} else {
			model.addAttribute("messages", "用户账号密码有误！");
			return "login";
		}

	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("employee");
		return "redirect:admin/index";
	}
	
	@RequestMapping("/password-edit")
	public String passwordEdit(String password,int id,HttpSession session) {
		emp.updatePassword(password, id);
		session.removeAttribute("employee");
		return "redirect:admin/index";
	}

	@RequestMapping("/login")
	public String login() {
		return "login";
	}
}
